package pers.corvey.studyjava.model.form;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;

import org.springframework.web.multipart.MultipartFile;

import pers.corvey.studyjava.MyConstant;
import pers.corvey.studyjava.model.Attachment;
import pers.corvey.studyjava.model.Example;
import pers.corvey.studyjava.util.EncodingUtils;
import pers.corvey.studyjava.util.FileUtils;
import pers.corvey.studyjava.util.ToStringUtils;

public class ExampleForm implements MyValidatable {

    @NotNull(message="教程标题不能为空！")
    private String title;
    
    private String description;
    
    @NotNull(message="教程内容不能为空！")
    private String content;
    
    private MultipartFile[] attachments;
    
    private List<String> fileNames;
    
    @AssertTrue(message="文件个数与文件名必须一一对应！")
    private boolean fileNameNumCheck = false;
    
    public ExampleForm() {}
    
    public Example toExample() throws IOException {
        Example example = new Example();
        example.setTitle(getTitle());
        example.setDescription(getDescription());
        File file = FileUtils.saveString(MyConstant.DOC_FILE_PATH, content, "md");
        example.setDocFilePath(MyConstant.DOC_FILE_PATH + file.getName());
        System.out.println(123);
        example.setAttachment(toAttachments());
        return example;
    }
    
    private List<Attachment> toAttachments() throws IOException {
        final String fileDirPath = MyConstant.CLASS_STATIC_PATH + MyConstant.FILE_PATH;
        final String fileBackupDirPath = MyConstant.BACKUP_STATIC_PATH + MyConstant.FILE_PATH;
        List<Attachment> ret = new ArrayList<>();
        for (int i=0; i<attachments.length; ++i) {
            MultipartFile multipartFile = attachments[i];
            String attachmentName = fileNames.get(i);
            String fileName = EncodingUtils.encodeByMD5(multipartFile.getBytes());
            String extension = FileUtils.getFileExtension(multipartFile.getOriginalFilename());
            File file = new File(fileDirPath, fileName + extension);
            multipartFile.transferTo(file);
            Attachment attachment = new Attachment();
            attachment.setTitle(attachmentName);
            attachment.setFilePath(MyConstant.FILE_PATH + file.getName());
            ret.add(attachment);
            file = new File(fileBackupDirPath, file.getName());
            multipartFile.transferTo(file);
            System.out.println(i + "成功！");
        }
        return ret;
    }
    
    @Override
    public void validate() {
        fileNameNumCheck = attachments.length == fileNames.size();
        MyValidatable.super.validate();
    }
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public MultipartFile[] getAttachments() {
        return attachments;
    }

    public void setAttachments(MultipartFile[] attachments) {
        this.attachments = attachments;
    }

    public List<String> getFileNames() {
        return fileNames;
    }
    
    public void setFileNames(List<String> fileNames) {
      this.fileNames = fileNames;
    }
    
    @Override
    public String toString() {
        return ToStringUtils.objectToString(this);
    }
}
